# Title     : TODO
# Objective : TODO
# Created by: yz
# Created on: 4/9/2020

getParentNode <- function(pns) {
  pns %>%
    str_split(";") %>%
    unlist() %>%
    head(1)
}

getCalculateArg <- function(calculateConfigData, fieldName, index) {
  configGet(calculateConfigData, fieldName) %>%
    str_split(";") %>%
    unlist() %>%
    .[index + 1]
}

plotConfigJson <- fromJSON(opt$pc)
colorsStr <- plotConfigJson$color$colorsStr
customColors <- plotConfigJson$color$customColors
finalColorsStr <- if (!is.null(colorsStr)  && colorsStr == "custom") {
  customColors
} else {
  colorsStr
}
finalColors <- str_split(finalColorsStr, ":") %>%
  unlist()

legendTitle <- plotConfigJson$font$legendTitle
height <-plotConfigJson$layout$height %>%
  as.numeric()
width <- plotConfigJson$layout$width %>%
  as.numeric()
fontFamily <-plotConfigJson$font$fontFamily
rocShowLegend <-plotConfigJson$layout$showLegend %>%
  as.logical()
xTitleFont <- plotConfigJson$font$xTitleFont
yTitleFont <- plotConfigJson$font$yTitleFont
xFont <- plotConfigJson$font$xFont
yFont <- plotConfigJson$font$yFont
legendFont <-plotConfigJson$font$legendFont
legendTitleFont <-plotConfigJson$font$legendTitleFont
mainTitleFont <-plotConfigJson$font$mainTitleFont
mainTitle <-plotConfigJson$font$mainTitle

getBasicPlotArg <- function(p,plotConfigData) {
  p <- p +
    theme(plot.title = element_text(hjust = 0.5, size = mainTitleFont), legend.text = element_text(size = legendFont),
          legend.title = element_text(size = legendTitleFont), axis.text.x = element_text(size = xFont),
          axis.title.x = element_text(size = xTitleFont), axis.text.y = element_text(size = yFont),
          axis.title.y = element_text(size = yTitleFont))

  if (!is.na(mainTitle)) {
    p <- p + ggtitle(mainTitle)
  }
  p
}

getfinalColors <- function(plotConfigJson) {
  colorsStr <- plotConfigJson$color$colorsStr
  customColors <- plotConfigJson$color$customColors
  finalColorsStr <- if (colorsStr == "custom") {
    customColors
  } else {
    colorsStr
  }
  finalColors <- str_split(finalColorsStr, ":") %>%
    unlist()
  finalColors
}
